package cn.xfdzx.android.apps.shop.activity;

import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessGoodsListAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessImageListAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessListAdapter;
import cn.xfdzx.android.apps.shop.adapter.FullyGridLayoutManager;
import cn.xfdzx.android.apps.shop.adapter.GridImageSelAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterMarketInfoBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.OrderDetailInfoBeen;
import cn.xfdzx.android.apps.shop.bean.OrderProcessBeen;
import cn.xfdzx.android.apps.shop.bean.ReturnReasonBean;
import cn.xfdzx.android.apps.shop.contract.AfterServiceContract;
import cn.xfdzx.android.apps.shop.presenter.AfterServicePresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

/*
/* 售后详情页面
 */
public class AfterServiceInfoActivity extends BaseMvpActivity<AfterServicePresenter> implements AfterServiceContract.View , View.OnClickListener{
    private RecyclerView recycler_goods;//商品列表
    private RecyclerView recycler_imgs;//列表图片
    private RecyclerView recycler_process;//进度列表
    private TextView tv_returnmoney;//退款金额
    private RelativeLayout rl_returnmoney;//退款金额布局
    private TextView tv_problemtype;//问题类型
    private TextView tv_problem;//问题
    private TextView tv_cancleafter;//取消
    private TextView tv_updateafter;//更改
    private ImageView iv_back;

    private TextView tv_state;//状态
    private TextView tv_statedes;//状态描述
    private TextView tv_time;//时间
    private TextView tv_state_start;//开始状态
    private TextView tv_time_start;//开始状态描述
    private TextView tv_statedes_start;//开始时间
    private RelativeLayout rl_end;
    private LinearLayout llviewcircle_end;
    private ImageView imgcircle_start,imgcircle;//开始和结束左侧对号按钮
    private View viewcircle_start,viewcircle;//开始和结束左侧圆点
    private TextView viewcirclebottom;

    private AfterServiceProcessGoodsListAdapter afterServiceGoodsListAdapter;
    private String type ="0";//区分0零售还是1批发
    private String orderId;//订单id
    private String applyId;//申请售后的ID
    private List<OrderProcessBeen.DataBean.ApplyGoodsBean> cartInfo = new ArrayList<>();//商品列表
    private AfterServiceProcessImageListAdapter imageListAdapter;
    private List<String> refund_reason_wap_img;
    private Dialog dialog;//确认弹窗
    private AfterServiceProcessListAdapter processListAdapter;
    private List<OrderProcessBeen.DataBean.ProgressBean> progress = new ArrayList<>();;

    @Override
    public int getLayoutId() {
        return R.layout.activity_afterservice_info;
    }

    @Override
    public void initView() {
        mPresenter = new AfterServicePresenter();
        mPresenter.attachView(this);
        type = getIntent().getStringExtra("type");
        orderId = getIntent().getStringExtra("orderId");
        applyId = getIntent().getStringExtra("applyId");
        recycler_goods = findViewById(R.id.recycle_goods);
        recycler_imgs = findViewById(R.id.recycler_imgs);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recycler_goods.setLayoutManager(layoutManager);
        tv_returnmoney = findViewById(R.id.tv_returnmoney);
        rl_returnmoney = findViewById(R.id.rl_returnmoney);
        tv_problemtype = findViewById(R.id.tv_problemtype);
        tv_problem = findViewById(R.id.tv_problem);
        tv_cancleafter = findViewById(R.id.tv_cancleafter);
        tv_updateafter = findViewById(R.id.tv_updateafter);
        iv_back = findViewById(R.id.iv_back);

        View headView  = LayoutInflater.from(this).inflate(R.layout.activity_afterservice_info_head, recycler_goods, false);
        recycler_process = headView.findViewById(R.id.recycler_process);
        tv_state = headView.findViewById(R.id.tv_state);
        tv_statedes = headView.findViewById(R.id.tv_statedes);
        tv_time = headView.findViewById(R.id.tv_time);
        tv_state_start = headView.findViewById(R.id.tv_state_start);
        tv_time_start = headView.findViewById(R.id.tv_time_start);
        tv_statedes_start = headView.findViewById(R.id.tv_statedes_start);
        rl_end = headView.findViewById(R.id.rl_end);
        llviewcircle_end = headView.findViewById(R.id.llviewcircle_end);
        imgcircle_start = headView.findViewById(R.id.imgcircle_start);
        imgcircle = headView.findViewById(R.id.imgcircle);
        viewcircle_start = headView.findViewById(R.id.viewcircle_start);
        viewcircle = headView.findViewById(R.id.viewcircle);
        viewcirclebottom = headView.findViewById(R.id.viewcirclebottom);

        tv_cancleafter.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        tv_updateafter.setOnClickListener(this);
        afterServiceGoodsListAdapter = new AfterServiceProcessGoodsListAdapter(cartInfo);
        recycler_goods.setAdapter(afterServiceGoodsListAdapter);
        afterServiceGoodsListAdapter.addHeaderView(headView);

        Map<String,Object> map = new HashMap<>();
        map.put("applyId",applyId);
        mPresenter.getAfterServiceProgress(map);
        initListener();
    }

    private void initListener() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        recycler_imgs.setLayoutManager(manager);
        imageListAdapter = new AfterServiceProcessImageListAdapter(refund_reason_wap_img);
        recycler_imgs.setAdapter(imageListAdapter);

        dialog = Utils.addDialog(this, R.layout.dialog, R.string.confirmcancleafter, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,Object> map = new HashMap<>();
                map.put("orderId",orderId);
                map.put("applyId",applyId);
                mPresenter.getAfterServiceCancle(map);
                dialog.dismiss();
            }
        });

        processListAdapter = new AfterServiceProcessListAdapter(progress);
        recycler_process.setLayoutManager(new LinearLayoutManager(this));
        recycler_process.setAdapter(processListAdapter);
    }
    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }

    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    /**上传单张回调*/
    @Override
    public void onUploadMoreImgSuccess(AfterServiceUpLoadMoreImgBean bean) {

    }

    /**上传单张回调*/
    @Override
    public void onUploadSingImgSuccess(AfterServiceUpLoadSingImgBean bean) {

    }

    @Override
    public void getReturnReasonSuccess(ReturnReasonBean bean) {
    }

    @Override
    public void getAfterServiceRequestSuccess(OrderProcessBeen bean) {

    }

    @Override
    public void getAfterServiceCancleSuccess(ReturnReasonBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Toast.makeText(this, "已取消售后", Toast.LENGTH_SHORT).show();
            Map<String,Object> map = new HashMap<>();
            map.put("applyId",applyId);
            mPresenter.getAfterServiceProgress(map);
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getAfterServiceProgressSuccess(OrderProcessBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (EmptyUtil.isNotEmpty(bean.getData())){

                tv_problemtype.setText("问题类型:"+bean.getData().getReason());
                tv_problem.setText(bean.getData().getDesc()==null?"":bean.getData().getDesc());
                tv_returnmoney.setText("退款总金额: ¥"+bean.getData().getApply_price());
                afterServiceGoodsListAdapter.refreshDatas(bean.getData().getApplyGoods());
                progress = bean.getData().getProgress();
                processListAdapter.refreshDatas(progress);
                // 申请的状态  1 => 申请中  2 => 平台同意申请售后  3 => 平台拒绝申请  4 => 用户取消申请售后 5 => 退款中 6 => 退款成功
                if (bean.getData().getStatus() == 1){
                    tv_cancleafter.setVisibility(View.VISIBLE);
                }else {
                    tv_cancleafter.setVisibility(View.GONE);
                }

                refund_reason_wap_img = bean.getData().getImgs();
                imageListAdapter.refreshDatas(refund_reason_wap_img);
            }

        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getOrderDetailSuccess(OrderDetailInfoBeen bean) {

    }

    @Override
    public void getMarketAfterServiceRequestSucces(OrderProcessBeen bean) {

    }

    @Override
    public void getMarketAfterServiceCancleSucces(ReturnReasonBean bean) {

    }

    @Override
    public void getMarketAfterServiceInfoSucces(AfterMarketInfoBean bean) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null!=dialog){
            dialog.dismiss();
        }
        if (null!=mPresenter)
        mPresenter.detachView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_cancleafter:    //取消申请
                dialog.show();
                break;

        }
    }


}
